package com.leo.blog.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dao.HeadLineDao;
import com.leo.blog.dto.HeadLineExecution;
import com.leo.blog.entity.HeadLine;
import com.leo.blog.enums.headLineStateEnum;
import com.leo.blog.exceptions.HeadLineOperationException;
import com.leo.blog.service.HeadLineService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.PageCalculator;

@Service
public class HeadLineServiceImpl implements HeadLineService{
	@Autowired
	private HeadLineDao headLineDao;

	@Override
	@SystemLog(description = ConstantUtil.HEAD_LINE_ADD, userType = ConstantUtil.USERTYPE_ADMIN)
	public HeadLineExecution addHeadLine(String name, HeadLine headLine) throws HeadLineOperationException {
		//初始化时间
		headLine.setCreateTime(new Date());
		headLine.setUpdateTime(new Date());
		
		int effectedNum = headLineDao.insertHeadLine(headLine);
		if(effectedNum > 0) {
			return new HeadLineExecution(headLineStateEnum.SUCCESS);
		}else {
			return new HeadLineExecution(headLineStateEnum.OFFLINE);
		}
	}

	@Override
	public HeadLineExecution getHeadLineById(int headLineId) {
		HeadLine headLine = headLineDao.queryHeadLineById(headLineId);
		HeadLineExecution he = new HeadLineExecution();
		if(headLine != null) {
			he.setHeadLine(headLine);
			he.setState(headLineStateEnum.SUCCESS.getState());
		}else {
			he.setState(headLineStateEnum.OFFLINE.getState());
		}
		return he;
	}

	@Override
	@SystemLog(description = ConstantUtil.HEAD_LINE_UPDATE, userType = ConstantUtil.USERTYPE_ADMIN)
	public HeadLineExecution updateHeadLine(String name, HeadLine headLine) {
		headLine.setUpdateTime(new Date());//初始化更新时间
		
		int effectedNum = headLineDao.updateHeadLine(headLine);
		if(effectedNum > 0) {
			return new HeadLineExecution(headLineStateEnum.SUCCESS);
		}else {
			return new HeadLineExecution(headLineStateEnum.OFFLINE);
		}
	}

	@Override
	@SystemLog(description = ConstantUtil.HEAD_LINE_DELETE, userType = ConstantUtil.USERTYPE_ADMIN)
	public HeadLineExecution deleteHeadLineById(String name, int headLineId) {
		int effectedNum = headLineDao.deleteHeadLineById(headLineId);
		if(effectedNum > 0) {
			return new HeadLineExecution(headLineStateEnum.SUCCESS);
		}else {
			return new HeadLineExecution(headLineStateEnum.OFFLINE);
		}
	}

	@Override
	public HeadLineExecution getHeadLineListByPage(HeadLine headLineCondition, int pageIndex, int pageSize) {
		HeadLineExecution he = new HeadLineExecution();
		
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据查询条件,待用dao层返回相关类型列表
		List<HeadLine> headLineList = headLineDao.queryHeadLineListByPage(headLineCondition, rowIndex, pageSize);
		int count = headLineDao.queryHeadLineListByPageCount(headLineCondition);
		if(headLineList != null && count > 0) {
			he.setHeadLineList(headLineList);
			he.setCount(count);
			he.setState(headLineStateEnum.SUCCESS.getState());
		}else {
			he.setState(headLineStateEnum.OFFLINE.getState());
		}
		return he;
	}
	
}
